package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.BeanCopierUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.enums.NextNodeParamEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowNextNodeAssignee;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowNodeTempParamDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowNodeTempParamRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowNodeTempParam;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowNodeTempParamExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowNodeTempParamDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author liyang
 * @version 1.0
 * @date 2021-07-15 19:03:40
 **/
@Service
public class FlowNodeTempParamRepositoryImpl implements FlowNodeTempParamRepository {

    @Autowired
    private FlowNodeTempParamDAO flowNodeTempParamDAO;

    @Override
    public List<FlowNodeTempParamDomain> listNodeTempParam(
            String processInstanceId, String nodeCode, NextNodeParamEnum nextNodeParamEnum) {
        FlowNodeTempParamExample flowNodeTempParamExample = new FlowNodeTempParamExample();
        flowNodeTempParamExample.createCriteria().andProcessInstanceIdEqualTo(processInstanceId).
                andNodeCodeEqualTo(nodeCode).andParamCodeEqualTo(nextNodeParamEnum.getCode());

        List<FlowNodeTempParam> flowNodeTempParamList = flowNodeTempParamDAO.selectByExample(flowNodeTempParamExample);
        if (Objects.nonNull(flowNodeTempParamList) && flowNodeTempParamList.size() > 0){
            List<FlowNodeTempParamDomain> flowNodeTempParamDomainList = new ArrayList<FlowNodeTempParamDomain>(flowNodeTempParamList.size());
            flowNodeTempParamList.stream().forEach(flowNodeTempParam -> {
                FlowNodeTempParamDomain flowNodeTempParamDomain = new FlowNodeTempParamDomain();
                BeanCopierUtils.copyProperties(flowNodeTempParam, flowNodeTempParamDomain);
                flowNodeTempParamDomainList.add(flowNodeTempParamDomain);
            });
            return flowNodeTempParamDomainList;
        }
        return null;
    }

    @Override
    public int create(FlowNodeTempParamDomain flowNodeTempParamDomain) {
        FlowNodeTempParam flowNodeTempParam = new FlowNodeTempParam();
        BeanCopierUtils.copyProperties(flowNodeTempParamDomain,flowNodeTempParam);
        return flowNodeTempParamDAO.insert(flowNodeTempParam);
    }

    @Override
    public void update(FlowNodeTempParamDomain flowNodeTempParamDomain) {
        FlowNodeTempParam flowNodeTempParam = new FlowNodeTempParam();
        BeanCopierUtils.copyProperties(flowNodeTempParamDomain,flowNodeTempParam);
        flowNodeTempParamDAO.updateByPrimaryKeySelective(flowNodeTempParam);
    }


    @Override
    public void deleteByExample(FlowNodeTempParamDomain flowNodeTempParamDomain) {
        FlowNodeTempParamExample flowNodeTempParamExample = new FlowNodeTempParamExample();
        flowNodeTempParamExample.createCriteria().andProcessInstanceIdEqualTo(flowNodeTempParamDomain.getProcessInstanceId())
                .andNodeCodeEqualTo(flowNodeTempParamDomain.getNodeCode());
        flowNodeTempParamDAO.deleteByExample(flowNodeTempParamExample);
    }
}
